<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>正则表达式 | To Share</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/share/favicon.ico">
    <meta name="description" content="分享风雨，享受阳光">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/share/assets/css/0.styles.d7affcea.css" as="style"><link rel="preload" href="/share/assets/js/app.706684de.js" as="script"><link rel="preload" href="/share/assets/js/3.4685d505.js" as="script"><link rel="preload" href="/share/assets/js/1.b12738fb.js" as="script"><link rel="preload" href="/share/assets/js/68.c6c9e158.js" as="script"><link rel="preload" href="/share/assets/js/9.4e81f08e.js" as="script"><link rel="prefetch" href="/share/assets/js/10.9e95ce7b.js"><link rel="prefetch" href="/share/assets/js/11.e99410dc.js"><link rel="prefetch" href="/share/assets/js/12.e9afe07c.js"><link rel="prefetch" href="/share/assets/js/13.078dd281.js"><link rel="prefetch" href="/share/assets/js/14.6575b8f0.js"><link rel="prefetch" href="/share/assets/js/15.d942bb10.js"><link rel="prefetch" href="/share/assets/js/16.fd8cc40c.js"><link rel="prefetch" href="/share/assets/js/17.f1b1f2c6.js"><link rel="prefetch" href="/share/assets/js/18.b312ce79.js"><link rel="prefetch" href="/share/assets/js/19.a99d170f.js"><link rel="prefetch" href="/share/assets/js/20.cda2b911.js"><link rel="prefetch" href="/share/assets/js/21.d6062607.js"><link rel="prefetch" href="/share/assets/js/22.5a50eee4.js"><link rel="prefetch" href="/share/assets/js/23.be601eed.js"><link rel="prefetch" href="/share/assets/js/24.3d0b9811.js"><link rel="prefetch" href="/share/assets/js/25.c1c35d96.js"><link rel="prefetch" href="/share/assets/js/26.9a8b03ff.js"><link rel="prefetch" href="/share/assets/js/27.6c302f3e.js"><link rel="prefetch" href="/share/assets/js/28.5eb55960.js"><link rel="prefetch" href="/share/assets/js/29.8297842b.js"><link rel="prefetch" href="/share/assets/js/30.b453256b.js"><link rel="prefetch" href="/share/assets/js/31.6ce2e46f.js"><link rel="prefetch" href="/share/assets/js/32.cf3906ab.js"><link rel="prefetch" href="/share/assets/js/33.83680f76.js"><link rel="prefetch" href="/share/assets/js/34.dada508c.js"><link rel="prefetch" href="/share/assets/js/35.11a36f17.js"><link rel="prefetch" href="/share/assets/js/36.fc43ef74.js"><link rel="prefetch" href="/share/assets/js/37.af715eba.js"><link rel="prefetch" href="/share/assets/js/38.7ba3e6e1.js"><link rel="prefetch" href="/share/assets/js/39.cd1c9d09.js"><link rel="prefetch" href="/share/assets/js/4.c1f99423.js"><link rel="prefetch" href="/share/assets/js/40.8df0c044.js"><link rel="prefetch" href="/share/assets/js/41.e643a418.js"><link rel="prefetch" href="/share/assets/js/42.d809cd4f.js"><link rel="prefetch" href="/share/assets/js/43.5136999f.js"><link rel="prefetch" href="/share/assets/js/44.315a82bf.js"><link rel="prefetch" href="/share/assets/js/45.a4b04306.js"><link rel="prefetch" href="/share/assets/js/46.2e8ec77e.js"><link rel="prefetch" href="/share/assets/js/47.d6bde2ca.js"><link rel="prefetch" href="/share/assets/js/48.e8e85c15.js"><link rel="prefetch" href="/share/assets/js/49.fce2ff19.js"><link rel="prefetch" href="/share/assets/js/5.599dfd12.js"><link rel="prefetch" href="/share/assets/js/50.3918e713.js"><link rel="prefetch" href="/share/assets/js/51.c5ddf50e.js"><link rel="prefetch" href="/share/assets/js/52.b6653f73.js"><link rel="prefetch" href="/share/assets/js/53.c0420367.js"><link rel="prefetch" href="/share/assets/js/54.0f64522f.js"><link rel="prefetch" href="/share/assets/js/55.1f9ce6fb.js"><link rel="prefetch" href="/share/assets/js/56.0a2fc30f.js"><link rel="prefetch" href="/share/assets/js/57.3b81efcb.js"><link rel="prefetch" href="/share/assets/js/58.c617dcad.js"><link rel="prefetch" href="/share/assets/js/59.5239e9d4.js"><link rel="prefetch" href="/share/assets/js/6.4e5676a9.js"><link rel="prefetch" href="/share/assets/js/60.b614d70b.js"><link rel="prefetch" href="/share/assets/js/61.f45be005.js"><link rel="prefetch" href="/share/assets/js/62.52121455.js"><link rel="prefetch" href="/share/assets/js/63.9f699597.js"><link rel="prefetch" href="/share/assets/js/64.01c4bf37.js"><link rel="prefetch" href="/share/assets/js/65.0f31fd4b.js"><link rel="prefetch" href="/share/assets/js/66.55dbbe80.js"><link rel="prefetch" href="/share/assets/js/67.fd9ba6df.js"><link rel="prefetch" href="/share/assets/js/69.91757641.js"><link rel="prefetch" href="/share/assets/js/7.b8928c33.js"><link rel="prefetch" href="/share/assets/js/70.12f0a894.js"><link rel="prefetch" href="/share/assets/js/71.88c99794.js"><link rel="prefetch" href="/share/assets/js/72.e1d99c59.js"><link rel="prefetch" href="/share/assets/js/73.aee6f06a.js"><link rel="prefetch" href="/share/assets/js/74.b47532b0.js"><link rel="prefetch" href="/share/assets/js/75.1f1ab66e.js"><link rel="prefetch" href="/share/assets/js/8.56deb6eb.js">
    <link rel="stylesheet" href="/share/assets/css/0.styles.d7affcea.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>To Share</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>分享风雨，享受阳光</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><!---->
            
          <span data-v-25ba6db2>2018 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/share/" class="home-link router-link-active"><!----> <span class="site-name">To Share</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/share/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/share/categories/docker/" class="nav-link"><i class="undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/book/" class="nav-link"><i class="undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/flex/" class="nav-link"><i class="undefined"></i>
  flex
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/git/" class="nav-link"><i class="undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/golang/" class="nav-link"><i class="undefined"></i>
  golang
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/gui/" class="nav-link"><i class="undefined"></i>
  gui
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/idea/" class="nav-link"><i class="undefined"></i>
  idea
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/linux/" class="nav-link"><i class="undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/mysql/" class="nav-link"><i class="undefined"></i>
  mysql
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/nodeJs/" class="nav-link"><i class="undefined"></i>
  nodeJs
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/php/" class="nav-link"><i class="undefined"></i>
  php
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/python/" class="nav-link"><i class="undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/site/" class="nav-link"><i class="undefined"></i>
  site
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/vue/" class="nav-link"><i class="undefined"></i>
  vue
</a></li></ul></div></div><div class="nav-item"><a href="/share/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/share/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Docs
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/share/docs/git/" class="nav-link"><i class="undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/vue/" class="nav-link"><i class="undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/flex/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/node/" class="nav-link"><i class="undefined"></i>
  nodeJs
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/docker/" class="nav-link"><i class="undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/php/" class="nav-link"><i class="undefined"></i>
  php
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/linux/" class="nav-link"><i class="undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/python/" class="nav-link router-link-active"><i class="undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/gui/" class="nav-link"><i class="undefined"></i>
  GUI
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/golang/" class="nav-link"><i class="undefined"></i>
  golang
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/idea/" class="nav-link"><i class="undefined"></i>
  idea
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/site/" class="nav-link"><i class="undefined"></i>
  site
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/book/" class="nav-link"><i class="undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/mysql/" class="nav-link"><i class="undefined"></i>
  mysql
</a></li></ul></div></div><div class="nav-item"><a href="https://gitee.com/zenglingchuan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><img src="/share/avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <!----> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>63</h3> <h6 data-v-39576ba9>文章</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>51</h3> <h6 data-v-39576ba9>标签</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/share/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/share/categories/docker/" class="nav-link"><i class="undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/book/" class="nav-link"><i class="undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/flex/" class="nav-link"><i class="undefined"></i>
  flex
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/git/" class="nav-link"><i class="undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/golang/" class="nav-link"><i class="undefined"></i>
  golang
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/gui/" class="nav-link"><i class="undefined"></i>
  gui
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/idea/" class="nav-link"><i class="undefined"></i>
  idea
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/linux/" class="nav-link"><i class="undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/mysql/" class="nav-link"><i class="undefined"></i>
  mysql
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/nodeJs/" class="nav-link"><i class="undefined"></i>
  nodeJs
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/php/" class="nav-link"><i class="undefined"></i>
  php
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/python/" class="nav-link"><i class="undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/site/" class="nav-link"><i class="undefined"></i>
  site
</a></li><li class="dropdown-item"><!----> <a href="/share/categories/vue/" class="nav-link"><i class="undefined"></i>
  vue
</a></li></ul></div></div><div class="nav-item"><a href="/share/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/share/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Docs
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/share/docs/git/" class="nav-link"><i class="undefined"></i>
  git
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/vue/" class="nav-link"><i class="undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/flex/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/node/" class="nav-link"><i class="undefined"></i>
  nodeJs
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/docker/" class="nav-link"><i class="undefined"></i>
  docker
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/php/" class="nav-link"><i class="undefined"></i>
  php
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/linux/" class="nav-link"><i class="undefined"></i>
  linux
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/python/" class="nav-link router-link-active"><i class="undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/gui/" class="nav-link"><i class="undefined"></i>
  GUI
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/golang/" class="nav-link"><i class="undefined"></i>
  golang
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/idea/" class="nav-link"><i class="undefined"></i>
  idea
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/site/" class="nav-link"><i class="undefined"></i>
  site
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/book/" class="nav-link"><i class="undefined"></i>
  book
</a></li><li class="dropdown-item"><!----> <a href="/share/docs/mysql/" class="nav-link"><i class="undefined"></i>
  mysql
</a></li></ul></div></div><div class="nav-item"><a href="https://gitee.com/zenglingchuan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav> <ul class="sidebar-links"><li><a href="/share/docs/python/" aria-current="page" class="sidebar-link">python环境搭建</a></li><li><a href="/share/docs/python/sql.html" class="sidebar-link">原生sql操作封装</a></li><li><a href="/share/docs/python/bug.html" class="sidebar-link">python常见问题</a></li><li><a href="/share/docs/python/regular.html" aria-current="page" class="active sidebar-link">正则表达式</a></li><li><a href="/share/docs/python/string.html" class="sidebar-link">python常用函数</a></li><li><a href="/share/docs/python/flask.html" class="sidebar-link">Flask</a></li><li><a href="/share/docs/python/file.html" class="sidebar-link">文件处理</a></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>正则表达式</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><!---->
            
          <span data-v-25ba6db2>2018 - </span>
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">正则表达式</h1> <div data-v-f875f3fc><!----> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>2022/5/28</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>正则</span><span class="tag-item" data-v-f875f3fc>python</span></i></div></div> <div class="theme-reco-content content__default"><div id="boxx" data-v-f4ca0dac><div data-v-f4ca0dac><p v-if="true" class="custom-block-title" data-v-f4ca0dac></p> <p v-if="true" data-v-f4ca0dac></p></div></div> <div class="custom-block danger"><p class="title">注意</p><p>正则表达式，是一种语法，用来描述你想搜索的字符串的特征。</p> <p><a href="https://regex101.com/" target="_blank" rel="noopener noreferrer">正则表达式在线验证地址<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div><img src="/share/python/12.png" alt="foo"> <h2 id="入门引导案例"><a href="#入门引导案例" class="header-anchor">#</a> 入门引导案例</h2> <p>一个文本文件里面存储了 一些市场职位信息，格式如下所示</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>Python3 高级开发工程师 上海互教教育科技有限公司上海-浦东新区2万/月02-18满员
测试开发工程师（C++/python） 上海墨鹍数码科技有限公司上海-浦东新区2.5万/每月02-18未满员
Python3 开发工程师 上海德拓信息技术股份有限公司上海-徐汇区1.3万/每月02-18剩余11人
测试开发工程师（Python） 赫里普（上海）信息科技有限公司上海-浦东新区1.1万/每月02-18剩余5人
Python高级开发工程师 上海行动教育科技股份有限公司上海-闵行区2.8万/月02-18剩余255人
python开发工程师 上海优似腾软件开发有限公司上海-浦东新区2.5万/每月02-18满员
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>现在，我们需要写一个程序，从这些文本里面抓取 所有职位的薪资。</p> <p>就是要获取这样的结果</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>2
2.5
1.3
1.1
2.8
2.5
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>传统做法, 处理这个字符串, 分析这里面的规律，可以发现，<code>薪资的数字</code> 后面 都有关键字 <code>万/月</code> 或者 <code>万/每月</code></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''
Python3 高级开发工程师 上海互教教育科技有限公司上海-浦东新区2万/月02-18满员
测试开发工程师（C++/python） 上海墨鹍数码科技有限公司上海-浦东新区2.5万/每月02-18未满员
Python3 开发工程师 上海德拓信息技术股份有限公司上海-徐汇区1.3万/每月02-18剩余11人
测试开发工程师（Python） 赫里普（上海）信息科技有限公司上海-浦东新区1.1万/每月02-18剩余5人
Python高级开发工程师 上海行动教育科技股份有限公司上海-闵行区2.8万/月02-18剩余255人
python开发工程师 上海优似腾软件开发有限公司上海-浦东新区2.5万/每月02-18满员
'''</span>

<span class="token comment"># 将文本内容按行放入列表</span>
lines <span class="token operator">=</span> content<span class="token punctuation">.</span>splitlines<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> line <span class="token keyword">in</span> lines<span class="token punctuation">:</span>
    <span class="token comment"># 查找'万/月' 在 字符串中什么地方</span>
    pos2 <span class="token operator">=</span> line<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'万/月'</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> pos2 <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
        <span class="token comment"># 查找'万/每月' 在 字符串中什么地方</span>
        pos2 <span class="token operator">=</span> line<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'万/每月'</span><span class="token punctuation">)</span>
        <span class="token comment"># 都找不到</span>
        <span class="token keyword">if</span> pos2 <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">:</span>
            <span class="token keyword">continue</span>

    <span class="token comment"># 执行到这里，说明可以找到薪资关键字</span>
    <span class="token comment"># 接下来分析 薪资 数字的起始位置</span>
    <span class="token comment"># 方法是 找到 pos2 前面薪资数字开始的位置</span>
    idx <span class="token operator">=</span> pos2<span class="token operator">-</span><span class="token number">1</span>

    <span class="token comment"># 只要是数字或者小数点，就继续往前面找</span>
    <span class="token keyword">while</span> line<span class="token punctuation">[</span>idx<span class="token punctuation">]</span><span class="token punctuation">.</span>isdigit<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">or</span> line<span class="token punctuation">[</span>idx<span class="token punctuation">]</span><span class="token operator">==</span><span class="token string">'.'</span><span class="token punctuation">:</span>
        idx <span class="token operator">-=</span> <span class="token number">1</span>

    <span class="token comment"># 现在 idx 指向 薪资数字前面的那个字，</span>
    <span class="token comment"># 所以薪资开始的 索引 就是 idx+1</span>
    pos1 <span class="token operator">=</span> idx <span class="token operator">+</span> <span class="token number">1</span>

    <span class="token keyword">print</span><span class="token punctuation">(</span>line<span class="token punctuation">[</span>pos1<span class="token punctuation">:</span>pos2<span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><p>输出结果</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
2
2.5
1.3
1.1
2.8
2.5
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>但这样太麻烦了, 我们可以用正则表达式解决</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''
Python3 高级开发工程师 上海互教教育科技有限公司上海-浦东新区2万/月02-18满员
测试开发工程师（C++/python） 上海墨鹍数码科技有限公司上海-浦东新区2.5万/每月02-18未满员
Python3 开发工程师 上海德拓信息技术股份有限公司上海-徐汇区1.3万/每月02-18剩余11人
测试开发工程师（Python） 赫里普（上海）信息科技有限公司上海-浦东新区1.1万/每月02-18剩余5人
Python高级开发工程师 上海行动教育科技股份有限公司上海-闵行区2.8万/月02-18剩余255人
python开发工程师 上海优似腾软件开发有限公司上海-浦东新区2.5万/每月02-18满员
'''</span>

<span class="token keyword">import</span> re
<span class="token keyword">for</span> one <span class="token keyword">in</span>  re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">r'([\d.]+)万/每{0,1}月'</span><span class="token punctuation">,</span> content<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>one<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>正则表达式，是一种语法，用来描述你想搜索的字符串的特征。</p> <p>这里指定了一个正则表达式</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">r'([\d.]+)万/每{0,1}月'</span><span class="token punctuation">,</span> content<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="custom-block danger"><p class="title">先理解下</p><p><code>([\d.]+)万/每{0,1}月</code> ，就是正则表达式字符串，指定了搜索子串的特征。</p> <ol><li><code>[]</code> 方括号里面表示任意选择方括号里面的一种都可以</li> <li><code>\d.</code> 中, <code>\d</code>表示数字, <code>\d.</code>就是表示以<code>数字</code>或者<code>数字.</code>开头的</li> <li><code>每{0,1}</code> 表示每可以出现0次或者出现1次</li> <li><code>([\d.]+)</code>小括号把<code>[\d.]+</code>括起来, 就是表示要把这个单独提取出来的意思</li></ol></div><p><code>findall</code>函数返回所有匹配的子串，放在一个列表中。</p> <div class="custom-block danger"><p class="title">注意</p><p>用正则表达式关键的地方在于， 如何写出正确的表达式语法</p></div><h2 id="正则表达式常见语法"><a href="#正则表达式常见语法" class="header-anchor">#</a> 正则表达式常见语法</h2> <blockquote><p>正则表达式本身就是一个字符串,只是这个字符串符合正则表达式的语法,它的字符都有特别的含义</p></blockquote> <div class="custom-block danger"><p class="title">注意</p><p>以后写正则条件反射就应该加一个<code>r</code></p></div><img src="/share/python/15.png" alt="foo"> <h3 id="普通字符"><a href="#普通字符" class="header-anchor">#</a> 普通字符</h3> <p>正则表达式里面的字符分为两大类:</p> <ol><li>一类是普通字符串, 表示直接匹配他们;</li> <li>另外一类是特殊字符, 术语叫 metacharacters（元字符）,它们出现在正则表达式字符串中，不是表示直接匹配他们， 而是表达一些特别的含义。这些特殊的元字符包括下面这些：</li></ol> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token punctuation">.</span> <span class="token operator">*</span> <span class="token operator">+</span> ? \ <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token operator">^</span> $ <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token operator">|</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>例子: 写在正则表达式里面的普通字符都是表示： 直接匹配它们。比如下面这个例子</p> <img src="/share/python/13.png" alt="foo"> <p>在线验证, 以后就不写代码了, 直接在线验证</p> <img src="/share/python/14.png" alt="foo"> <h2 id="特殊字符"><a href="#特殊字符" class="header-anchor">#</a> 特殊字符</h2> <blockquote><p>术语叫 metacharacters（元字符）, 它们出现在正则表达式字符串中，不是表示直接匹配他们， 而是表达一些特别的含义。这些特殊的元字符包括下面这些：</p></blockquote> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token punctuation">.</span> <span class="token operator">*</span> <span class="token operator">+</span> ? \ <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token operator">^</span> $ <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token operator">|</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="点-匹配所有字符"><a href="#点-匹配所有字符" class="header-anchor">#</a> 点-匹配所有字符</h3> <p><code>.</code> 表示要匹配除了 <code>换行符</code> 之外的任何 <code>单个</code> 字符。</p> <p>比如，你要从下面的文本中，选择出所有的颜色。</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>苹果是绿色的
橙子是橙色的
香蕉是黄色的
乌鸦是黑色的
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>也就是要找到所有 以 <code>色</code> 结尾，并且包括<code>前面的一个字符的</code> 词语。</p> <p>就可以这样写正则表达式 <code>.色</code> 。</p> <p>其中 <code>点</code> 代表了<code>任意的一个字符</code>， 注意是一个字符。</p> <p><code>.色</code> 合起来就表示 <code>要找</code> 任意一个字符 后面是 <code>色</code> 这个字， 合起来<code>两个字的字符串</code></p> <p>验证一下，如下图所示</p> <img src="/share/python/16.png" alt="foo"> <p>只要表达式正确，就可以写在Python代码中，如下所示</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token keyword">import</span> re

content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''
苹果是绿色的
橙子是橙色的
香蕉是黄色的
乌鸦是黑色的
'''</span>
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'.色'</span><span class="token punctuation">)</span> <span class="token comment"># 产生一个Pattern对象, 方便调用正则的各种方法</span>
<span class="token keyword">for</span> one <span class="token keyword">in</span> p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment"># findall找到所有的符合条件的文本字符串</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>one<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>输出:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
绿色
橙色
黄色
黑色
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="星号-重复匹配任意次-包括0次"><a href="#星号-重复匹配任意次-包括0次" class="header-anchor">#</a> 星号-重复匹配任意次(包括0次)</h3> <div class="custom-block danger"><p class="title">注意</p><p><code>*</code> 表示匹配前面的子表达式任意次，包括0次。</p></div><p>比如，你要从下面的文本中，选择每行逗号后面的字符串内容，包括逗号本身。注意，这里的逗号是中文的逗号。</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>苹果，是绿色的
橙子，是橙色的
香蕉，是黄色的
乌鸦，是黑色的
猴子，
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>就可以这样写正则表达式 <code>，.*</code>。紧跟在 <code>.</code> 后面， 表示<code>任意字符</code>可以<code>出现任意次</code>， 所以整个表达式的意思就是<code>在逗号后面的 所有字符，包括逗号</code></p> <p>验证一下，如下图所示</p> <img src="/share/python/17.png" alt="foo"> <p>特别是最后一行，猴子<code>逗号</code>后面没有其它字符了，但是<code>*</code>表示可以匹配<code>0</code>次， 所以表达式也是成立的。</p> <p>只要表达式正确，就可以写在Python代码中，如下所示</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''苹果，是绿色的
橙子，是橙色的
香蕉，是黄色的
乌鸦，是黑色的
猴子，'''</span>

<span class="token keyword">import</span> re
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'，.*'</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> one <span class="token keyword">in</span>  p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>one<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>输出:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
，是绿色的
，是橙色的
，是黄色的
，是黑色的
，
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><div class="custom-block danger"><p class="title">注意</p><p>注意， .* 在正则表达式中非常常见，表示匹配任意字符任意次数。</p> <p>当然这个 * 前面不是非得是 点 ，也可以是其它字符，比如</p></div><img src="/share/python/18.png" alt="foo"> <h3 id="加号-重复匹配多次-不包括0次"><a href="#加号-重复匹配多次-不包括0次" class="header-anchor">#</a> 加号-重复匹配多次(不包括0次)</h3> <div class="custom-block danger"><p class="title">注意</p><p><code>+</code> 表示匹配前面的子表达式一次或多次，不包括0次。 和<code>*</code>的用法基本一样, 不同的就是<code>*</code>可以匹配0次</p></div><p>比如，还是上面的例子，你要从文本中，选择每行逗号后面的字符串内容，包括逗号本身。</p> <p>但是 添加一个条件， 如果逗号后面 没有内容，就不要选择了。</p> <p>比如，下面的文本中，最后一行逗号后面 没有内容，就不要选择了。</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>苹果，是绿色的
橙子，是橙色的
香蕉，是黄色的
乌鸦，是黑色的
猴子，
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>就可以这样写正则表达式 <code>，.+</code> 。</p> <img src="/share/python/19.png" alt="foo"> <p>最后一行，猴子逗号后面没有其它字符了，<code>+</code>表示至少匹配1次， 所以最后一行没有子串选中。</p> <h3 id="问号-匹配0次或1次"><a href="#问号-匹配0次或1次" class="header-anchor">#</a> 问号-匹配0次或1次</h3> <div class="custom-block danger"><p class="title">注意</p><p>? 表示匹配前面的子表达式0次或1次。</p></div><p>比如，还是上面的例子，你要从文本中，选择每行逗号后面的1个字符，也包括逗号本身。</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>苹果，绿色的
橙子，橙色的
香蕉，黄色的
乌鸦，黑色的
猴子，
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>就可以这样写正则表达式 <code>，.?</code> 。</p> <p>验证一下，如下图所示</p> <img src="/share/python/22.png" alt="foo"> <p>最后一行，猴子逗号后面没有其它字符了，但是?表示匹配1次或0次， 所以最后一行也选中了一个逗号字符。</p> <h3 id="花括号-匹配指定次数"><a href="#花括号-匹配指定次数" class="header-anchor">#</a> 花括号-匹配指定次数</h3> <div class="custom-block danger"><p class="title">注意</p><p>花括号表示 前面的字符匹配 指定的次数</p></div><p>比如 ，下面的文本</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>红彤彤，绿油油，黑乎乎，绿油油油油
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>表达式 <code>油{3}</code> 就表示匹配 <code>连续的 油 字</code> <code>3次</code></p> <p>表达式 <code>油{3,4}</code> 就表示匹配 <code>连续的 油 字</code> <code>至少3次，至多 4 次</code></p> <p>就只能匹配 后面的，如下所示</p> <img src="/share/python/20.png" alt="foo"> <p>案例场景, 提取手机号码</p> <img src="/share/python/21.png" alt="foo"> <h3 id="贪婪模式和非贪婪模式"><a href="#贪婪模式和非贪婪模式" class="header-anchor">#</a> 贪婪模式和非贪婪模式</h3> <div class="custom-block danger"><p class="title">注意</p><p>在正则表达式中， <code>‘*’, ‘+’, ‘?’</code> 都是贪婪地，使用他们时，会尽可能多的匹配内容</p></div><p>我们要把下面的字符串中的所有html标签都提取出来</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>source <span class="token operator">=</span> <span class="token string">'&lt;html&gt;&lt;head&gt;&lt;title&gt;Title&lt;/title&gt;'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>得到这样的一个列表</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token punctuation">[</span><span class="token string">'&lt;html&gt;'</span><span class="token punctuation">,</span> <span class="token string">'&lt;head&gt;'</span><span class="token punctuation">,</span> <span class="token string">'&lt;title&gt;'</span><span class="token punctuation">,</span> <span class="token string">'&lt;/title&gt;'</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>很容易想到使用正则表达式 <code>&lt;.*&gt;</code></p> <p>写出如下代码</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>source <span class="token operator">=</span> <span class="token string">'&lt;html&gt;&lt;head&gt;&lt;title&gt;Title&lt;/title&gt;'</span>

<span class="token keyword">import</span> re
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'&lt;.*&gt;'</span><span class="token punctuation">)</span>

<span class="token keyword">print</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>source<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>但是运行结果，却是</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token punctuation">[</span><span class="token string">'&lt;html&gt;&lt;head&gt;&lt;title&gt;Title&lt;/title&gt;'</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>怎么回事？ 原来 在正则表达式中， <code>‘*’, ‘+’, ‘?’</code> 都是贪婪地，使用他们时，会尽可能多的匹配内容</p> <p>所以，<code>&lt;.*&gt;</code> 中的 <code>星号（表示任意次数的重复）</code>，一直匹配到了 <code>字符串最后的 &lt;/title&gt;</code> 里面的<code>e</code>。</p> <p>解决这个问题，就需要使用<code>非贪婪模式</code>，也就是在<code>星号</code>后面加上 <code>?</code> ，变成这样 <code>&lt;.*?&gt;</code></p> <p>代码改为</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>source <span class="token operator">=</span> <span class="token string">'&lt;html&gt;&lt;head&gt;&lt;title&gt;Title&lt;/title&gt;'</span>

<span class="token keyword">import</span> re
<span class="token comment"># 注意多出的问号</span>
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'&lt;.*?&gt;'</span><span class="token punctuation">)</span>

<span class="token keyword">print</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>source<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>再运行一遍，就可以了</p> <h3 id="对元字符进行转义"><a href="#对元字符进行转义" class="header-anchor">#</a> 对元字符进行转义 <span style="color:red;font-weight:bold;"> \ </span></h3> <p>元字符有</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>. * + ? \ [ ] ^ $ { } | ( )
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="custom-block danger"><p class="title">注意</p><p>反斜杠<span style="color:red;font-weight:bold;"> \ </span>在正则表达式中有多种用途。</p></div><p>比如，我们要在下面的文本中搜索 所有点前面的字符串，也包含点本身</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>苹果.是绿色的
橙子.是橙色的
香蕉.是黄色的
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果，我们这样写正则表达式 <code>.*.</code> ， 聪明的你肯定发现不对劲。</p> <p>因为 点 是一个 元字符， 直接出现在正则表达式中，表示匹配任意的单个字符， 不能表示 . 这个字符本身的意思了。</p> <hr> <span style="color:red;font-weight:bold;"> 怎么办呢？ </span> <p>如果我们要搜索的内容本身就包含元字符，就可以使用 反斜杠进行转义。</p> <p>这里我们就应用使用这样的表达式：   <code>.*\.</code></p> <p>示例:</p> <img src="/share/python/23.png" alt="foo"> <h3 id="匹配某种字符类型"><a href="#匹配某种字符类型" class="header-anchor">#</a> 匹配某种字符类型</h3> <p>反斜杠后面接一些字符，表示匹配 <span style="color:red;font-weight:bold;"> 某种类型 </span> 的一个字符。</p> <p>比如:</p> <ul><li>\d 匹配0-9之间任意一个数字字符，等价于表达式 [0-9]</li> <li>\D 匹配任意一个不是0-9之间的数字字符，等价于表达式 [^0-9]</li> <li>\s 匹配任意一个空白字符，包括 空格、tab、换行符等，等价于表达式 [\t\n\r\f\v]</li> <li>\S 匹配任意一个非空白字符，等价于表达式 [^ \t\n\r\f\v]</li> <li>\w 匹配任意一个文字字符，包括大小写字母、数字、下划线，等价于表达式 [a-zA-Z0-9_],缺省情况也包括 Unicode文字字符，如果指定 ASCII 码标记，则只包括ASCII字母</li> <li>\W 匹配任意一个非文字字符，等价于表达式 [^a-zA-Z0-9_]</li></ul> <div class="custom-block danger"><p class="title">注意</p><p>反斜杠也可以用在方括号里面，比如 [\s,.] 表示匹配 ： 任何空白字符， 或者逗号，或者点</p></div><div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token keyword">import</span> re

content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''
小明
any
王五
'''</span>
<span class="token comment"># 只匹配字母</span>
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'\w{2,4}'</span><span class="token punctuation">,</span> re<span class="token punctuation">.</span>ASCII<span class="token punctuation">)</span>
<span class="token keyword">for</span> one <span class="token keyword">in</span> p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>one<span class="token punctuation">)</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>输出:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
any
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="方括号-匹配几个字符之一"><a href="#方括号-匹配几个字符之一" class="header-anchor">#</a> 方括号-匹配几个字符之一</h3> <blockquote><p>方括号表示要匹配 <span style="color:red;font-weight:bold;"> 指定的几个字符之一 </span></p></blockquote> <p><span style="color:blue;font-weight:bold;"> 比如 </span></p> <ul><li>[abc] 可以匹配 a, b, 或者 c 里面的任意一个字符。等价于 [a-c] 。[a-c] 中间的 <span style="color:blue;font-weight:bold;"> - </span> 表示一个范围从a 到 c。</li> <li>[1-3] 表示匹配从1到3</li> <li>[A-D] 表示匹配从A到D</li> <li>如果你想匹配所有的小写字母，可以使用 [a-z]</li></ul> <hr> <span style="color:blue;font-weight:bold;"> 一些 元字符 在 方括号内 失去了魔法， 变得和普通字符一样了。 </span> <ul><li>[akm.] 匹配 a k m . 里面任意一个字符, 这里 . 在括号里面不在表示 匹配任意字符了，而就是表示匹配 . 这个 字符;</li></ul> <hr> <span style="color:blue;font-weight:bold;"> 如果在方括号中使用 ^ ， 表示 非 方括号里面的字符集合。 </span> <ul><li>[\d] 表示匹配数字字符</li> <li>[^\d] 表示匹配非数字字符</li> <li>[^abc] 表示匹配除了 a b c 之外的字符</li></ul> <h3 id="起始、结尾位置-和-单行、多行模式"><a href="#起始、结尾位置-和-单行、多行模式" class="header-anchor">#</a> 起始、结尾位置 和 单行、多行模式</h3> <p><span style="color:blue;font-weight:bold;">  ^  </span> 表示匹配文本的 开头 位置。 这个符号^在方括号中表示非;</p> <p>正则表达式可以设定 <span style="color:blue;font-weight:bold;">  单行模式  </span> 和 <span style="color:blue;font-weight:bold;">  多行模式  </span></p> <p>如果是 <span style="color:blue;font-weight:bold;">  单行模式  </span> ，表示匹配 <span style="color:red;font-weight:bold;">  整个文本  </span> 的开头位置。</p> <p>如果是 <span style="color:blue;font-weight:bold;">  多行模式  </span> ，表示匹配 <span style="color:red;font-weight:bold;">  文本每行  </span> 的开头位置。</p> <hr>
比如，下面的文本中，每行最前面的数字表示水果的编号，最后的数字表示价格
<div class="language-text line-numbers-mode"><pre class="language-text"><code>001-苹果价格-60，
002-橙子价格-70,
003-香蕉价格-80,
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果我们要提取所有的水果编号，用这样的正则表达式 <code>^\d+</code></p> <p>上面的正则表达式，使用在Python程序里面，如下所示</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''001-苹果价格-60
002-橙子价格-70
003-香蕉价格-80'''</span>

<span class="token keyword">import</span> re
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'^\d+'</span><span class="token punctuation">,</span> re<span class="token punctuation">.</span>M<span class="token punctuation">)</span>
<span class="token keyword">for</span> one <span class="token keyword">in</span>  p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>one<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="custom-block danger"><p class="title">注意</p><p>compile 的第二个参数 re.M ，指明了使用多行模式，</p></div><p>运行结果如下</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>001
002
003
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果，去掉 compile 的第二个参数 re.M， 运行结果如下</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>001
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>就只有第一行了。</p> <p>因为单行模式下，^ 只会匹配整个文本的开头位置。</p> <hr> <span style="color:blue;font-weight:bold;">  $  </span> 表示匹配文本的 <span style="color:blue;font-weight:bold;">  结尾  </span> 位置。
<p>如果是 <span style="color:blue;font-weight:bold;">  单行模式  </span> ，表示匹配 <span style="color:red;font-weight:bold;">  整个文本  </span> 的结尾位置。</p> <p>如果是 <span style="color:blue;font-weight:bold;">  多行模式  </span> ，表示匹配 <span style="color:red;font-weight:bold;">  文本每行  </span> 的结尾位置。</p> <p>比如，下面的文本中，每行最前面的数字表示水果的编号，最后的数字表示价格</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>001-苹果价格-60，
002-橙子价格-70,
003-香蕉价格-80,
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果我们要提取所有的水果编号，用这样的正则表达式 <code>\d+$</code></p> <p>对应代码</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''001-苹果价格-60
002-橙子价格-70
003-香蕉价格-80'''</span>

<span class="token keyword">import</span> re
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'\d+$'</span><span class="token punctuation">,</span> re<span class="token punctuation">.</span>MULTILINE<span class="token punctuation">)</span>
<span class="token keyword">for</span> one <span class="token keyword">in</span>  p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>one<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>注意，compile 的第二个参数 re.MULTILINE ，指明了使用多行模式，</p> <p>运行结果如下</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>60
70
80
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果，去掉 compile 的第二个参数 re.MULTILINE， 运行结果如下</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>80
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>就只有最后一行了。</p> <p>因为单行模式下，$ 只会匹配整个文本的结束位置。</p> <h3 id="竖线-匹配其中之一"><a href="#竖线-匹配其中之一" class="header-anchor">#</a> 竖线-匹配其中之一</h3> <p>竖线表示 匹配 其中之一 。</p> <img src="/share/python/24.png" alt="foo"> <p>特别要注意的是， 竖线在正则表达式的优先级是最低的， 这就意味着，竖线隔开的部分是一个整体</p> <p>比如 <span style="color:blue;font-weight:bold;">  绿色|橙  </span> 表示 要匹配是 <span style="color:blue;font-weight:bold;"> 绿色 </span>或者 <span style="color:blue;font-weight:bold;"> 橙 </span>，而不是 <span style="color:blue;font-weight:bold;"> 绿色 </span> 或者 <span style="color:blue;font-weight:bold;"> 绿橙 </span></p> <h3 id="括号-分组"><a href="#括号-分组" class="header-anchor">#</a> 括号-分组</h3> <blockquote><p>可以先写好正则表达式, 再给表达式分组, 提取分组信息</p></blockquote> <p>括号称之为 正则表达式的 <span style="color:blue;font-weight:bold;">  组选择  </span>。</p> <p><span style="color:blue;font-weight:bold;">  组  </span> 就是把 正则表达式 匹配的内容 里面 <span style="color:blue;font-weight:bold;">  其中的某些部分  </span> 标记为某个组。</p> <p>我们可以在 正则表达式中 标记 <span style="color:blue;font-weight:bold;">  多个  </span> 组</p> <hr> <p><span style="color:red;font-weight:bold;">  为什么要有组的概念呢？因为我们往往需要提取已经匹配的 内容里面的 某些部分的信心。  </span></p> <p>前面，我们有个例子，从下面的文本中，选择每行逗号前面的字符串，也 <span style="color:blue;font-weight:bold;">  包括逗号本身  </span> 。</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>苹果，苹果是绿色的
橙子，橙子是橙色的
香蕉，香蕉是黄色的
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>就可以这样写正则表达式 <code>^.*，</code></p> <p>但是，如果我们要求 <span style="color:blue;font-weight:bold;">  不要包括逗号  </span> 呢？</p> <p>当然不能直接 这样写 <code>^.*</code></p> <p>因为最后的逗号 是 特征 所在， 如果去掉它，就没法找 逗号前面的了。</p> <p>但是把逗号放在正则表达式中，又会包含逗号。</p> <p>解决问题的方法就是使用 组选择符 <span style="color:blue;font-weight:bold;"> 括号  </span>。</p> <p>我们这样写 <code>^(.*)，</code>,结果如下</p> <img src="/share/python/26.png" alt="foo"> <p>可以发现，我们把要从整个表达式中提取的部分放在括号中，这样 <code>水果</code> 的名字 就被单独的放在 组 <code>group</code> 中了。</p> <p>对应的Python代码如下</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''苹果，苹果是绿色的
橙子，橙子是橙色的
香蕉，香蕉是黄色的'''</span>

<span class="token keyword">import</span> re

p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'^(.*)，'</span><span class="token punctuation">,</span> re<span class="token punctuation">.</span>MULTILINE<span class="token punctuation">)</span>
<span class="token keyword">for</span> one <span class="token keyword">in</span> p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>one<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>输出:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
苹果
橙子
香蕉
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><hr> <span style="color:red;font-weight:bold;">  分组 还可以多次使用。  </span> <p>比如，我们要从下面的文本中，提取出每个人的 <code>名字</code> 和对应的 <code>手机号</code></p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>张三，手机号码15945678901
李四，手机号码13945677701
王二，手机号码13845666901
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>可以使用这样的正则表达式 <code>(.+)，.+(\d{11})</code></p> <img src="/share/python/27.png" alt="foo"> <p>可以写出如下的代码</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''张三，手机号码15945678901
李四，手机号码13945677701
王二，手机号码13845666901'''</span>

<span class="token keyword">import</span> re
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'^(.+)，.+(\d{11})'</span><span class="token punctuation">,</span> re<span class="token punctuation">.</span>MULTILINE<span class="token punctuation">)</span>
<span class="token keyword">for</span> one <span class="token keyword">in</span>  p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>one<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>输出:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
('张三', '15945678901')
('李四', '13945677701')
('王二', '13845666901')
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><hr> <hr> <hr> <p>当有多个分组的时候，我们可以使用 (?P&lt;分组名&gt;...) 这样的格式，给每个分组命名</p> <p>这样做的好处是，更方便后续的代码提取每个分组里面的内容</p> <img src="/share/python/28.png" alt="foo"> <p>代码实现:</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''张三，手机号码15945678901
李四，手机号码13945677701
王二，手机号码13845666901'''</span>

<span class="token keyword">import</span> re
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'^(?P&lt;name&gt;.+)，.+(?P&lt;phone&gt;\d{11})'</span><span class="token punctuation">,</span> re<span class="token punctuation">.</span>MULTILINE<span class="token punctuation">)</span>
<span class="token keyword">for</span> <span class="token keyword">match</span> <span class="token keyword">in</span>  p<span class="token punctuation">.</span>finditer<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token keyword">match</span><span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token keyword">match</span><span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token string">'phone'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>输出</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
张三
15945678901
李四
13945677701
王二
13845666901
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="让点匹配换行"><a href="#让点匹配换行" class="header-anchor">#</a> 让点匹配换行</h3> <p>前面说过, 点 是 不匹配换行符 的，可是有时候，特征 字符串就是跨行的，比如要找出下面文字中所有的职位名称</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;el&quot;</span><span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>p <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;t1&quot;</span><span class="token operator">&gt;</span>           
            <span class="token operator">&lt;</span>span<span class="token operator">&gt;</span>
                <span class="token operator">&lt;</span>a<span class="token operator">&gt;</span>Python开发工程师<span class="token operator">&lt;</span><span class="token operator">/</span>a<span class="token operator">&gt;</span>
            <span class="token operator">&lt;</span><span class="token operator">/</span>span<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>span <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;t2&quot;</span><span class="token operator">&gt;</span>南京<span class="token operator">&lt;</span><span class="token operator">/</span>span<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>span <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;t3&quot;</span><span class="token operator">&gt;</span><span class="token number">1.5</span><span class="token operator">-</span><span class="token number">2</span>万<span class="token operator">/</span>月<span class="token operator">&lt;</span><span class="token operator">/</span>span<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;el&quot;</span><span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>p <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;t1&quot;</span><span class="token operator">&gt;</span>
            <span class="token operator">&lt;</span>span<span class="token operator">&gt;</span>
                <span class="token operator">&lt;</span>a<span class="token operator">&gt;</span>java开发工程师<span class="token operator">&lt;</span><span class="token operator">/</span>a<span class="token operator">&gt;</span>
            <span class="token operator">&lt;</span><span class="token operator">/</span>span<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>span <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;t2&quot;</span><span class="token operator">&gt;</span>苏州<span class="token operator">&lt;</span><span class="token operator">/</span>span<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>span <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">&quot;t3&quot;</span><span class="token operator">&gt;</span><span class="token number">1.5</span><span class="token operator">-</span><span class="token number">2</span><span class="token operator">/</span>月<span class="token operator">&lt;</span><span class="token operator">/</span>span<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>如果你直接使用表达式 <code>class=\&quot;t1\&quot;&gt;.*?&lt;a&gt;(.*?)&lt;/a&gt;</code> 会发现匹配不上，因为 <code>t1</code> 和 <code>&lt;a&gt;</code> 之间有两个空行。</p> <hr> <p><span style="color:red;font-weight:bold;"> 这时你需要 点 也匹配换行符 ，可以使用 DOTALL 参数  </span></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>content <span class="token operator">=</span> <span class="token triple-quoted-string string">'''
&lt;div class=&quot;el&quot;&gt;
        &lt;p class=&quot;t1&quot;&gt;           
            &lt;span&gt;
                &lt;a&gt;Python开发工程师&lt;/a&gt;
            &lt;/span&gt;
        &lt;/p&gt;
        &lt;span class=&quot;t2&quot;&gt;南京&lt;/span&gt;
        &lt;span class=&quot;t3&quot;&gt;1.5-2万/月&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;el&quot;&gt;
        &lt;p class=&quot;t1&quot;&gt;
            &lt;span&gt;
                &lt;a&gt;java开发工程师&lt;/a&gt;
            &lt;/span&gt;
		&lt;/p&gt;
        &lt;span class=&quot;t2&quot;&gt;苏州&lt;/span&gt;
        &lt;span class=&quot;t3&quot;&gt;1.5-2/月&lt;/span&gt;
&lt;/div&gt;
'''</span>

<span class="token keyword">import</span> re
p <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">r'class=\&quot;t1\&quot;&gt;.*?&lt;a&gt;(.*?)&lt;/a&gt;'</span><span class="token punctuation">,</span> re<span class="token punctuation">.</span>DOTALL<span class="token punctuation">)</span>
<span class="token keyword">for</span> one <span class="token keyword">in</span>  p<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>one<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>输出:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
Python开发工程师
java开发工程师
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="入门案例解释"><a href="#入门案例解释" class="header-anchor">#</a> 入门案例解释</h2> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">\</span>d.<span class="token punctuation">]</span>+<span class="token punctuation">)</span>万/每<span class="token punctuation">{</span><span class="token number">0,1</span><span class="token punctuation">}</span>月
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li><code>[\d.]</code>  匹配<code>0到9</code>的数字,以及<code>.</code>, 从中匹配出一个字符</li> <li><code>[\d.]+</code> 匹配<code>数字</code>或者<code>.</code>, 匹配多次, 不包括0次, 可以匹配到1 11 11.11 等数字</li> <li><code>万/每{0,1}月</code>  是后面紧跟着的, 如果没有这个, 就会匹配到别的数字</li> <li><code>每{0,1}</code> 表示这个<code>每</code>字可以出现0次或者1次, 这个还可以用<code>?</code>来表示, 比如<code>每?</code>, <code>?</code>也是表示前面的字符出现0次或者1次</li></ul> <h2 id="切割字符串"><a href="#切割字符串" class="header-anchor">#</a> 切割字符串</h2> <p>字符串 对象的 <span style="color:red;font-weight:bold;"> split  </span> 方法只适用于 简单的字符串分割。 有时，你需要更加灵活的字符串切割。</p> <p>比如，我们需要从下面字符串中提取武将的名字。</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>names = '关羽; 张飞, 赵云,马超, 黄忠  李逵'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>我们发现这些名字之间， 有的是分号隔开，有的是逗号隔开，有的是空格隔开， 而且分割符号周围还有不定数量的空格
</p><hr> <span style="color:red;font-weight:bold;"> 这时，可以使用正则表达式里面的 split 方法：  </span><p></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token keyword">import</span> re

names <span class="token operator">=</span> <span class="token string">'关羽; 张飞, 赵云,   马超, 黄忠  李逵'</span>

namelist <span class="token operator">=</span> re<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">r'[;,\s]\s*'</span><span class="token punctuation">,</span> names<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>namelist<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>输出:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
['关羽', '张飞', '赵云', '马超', '黄忠', '李逵']
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>正则表达式 [;,\s]\s* 指定了，分割符为 分号、逗号、空格 里面的任意一种均可，并且 该符号周围可以有不定数量的空格。</p> <h2 id="字符串替换"><a href="#字符串替换" class="header-anchor">#</a> 字符串替换</h2> <h3 id="匹配模式替换"><a href="#匹配模式替换" class="header-anchor">#</a> 匹配模式替换</h3> <p>字符串 对象的 <span style="color:red;font-weight:bold;"> replace </span>  方法只适应于 简单的 替换。 有时，你需要更加灵活的字符串替换。</p> <p>比如，我们需要在下面这段文本中 所有的 链接中 找到所以 /avxxxxxx/ 这种 以 /av 开头，后面接一串数字， 这种模式的字符串。</p> <p>然后，这些字符串全部 替换为 /cn666666/ 。</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code>names <span class="token operator">=</span> <span class="token triple-quoted-string string">'''&lt;a href='https://www.baidu.com/av66771949/?p=1' target='_blank'&gt;学习以下内容&lt;/a&gt;
&lt;a href='https://www.baidu.com/av46349552/?p=125' target='_blank'&gt;学习以下内容&lt;/a&gt;
&lt;a href='https://www.baidu.com/av90571967/?p=33' target='_blank'&gt;学习以下内容&lt;/a&gt;
'''</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>被替换的内容不是固定的，所以没法用 字符串的replace方法。
</p><hr>
这时，可以使用正则表达式里面的 sub 方法：<p></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token keyword">import</span> re

names <span class="token operator">=</span> <span class="token triple-quoted-string string">'''&lt;a href='https://www.baidu.com/av66771949/?p=1' target='_blank'&gt;学习以下内容&lt;/a&gt;
&lt;a href='https://www.baidu.com/av46349552/?p=125' target='_blank'&gt;学习以下内容&lt;/a&gt;
&lt;a href='https://www.baidu.com/av90571967/?p=33' target='_blank'&gt;学习以下内容&lt;/a&gt;
'''</span>
newStr <span class="token operator">=</span> re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span><span class="token string">r'/(av\d+?)/'</span><span class="token punctuation">,</span> <span class="token string">'/cn666666/'</span> <span class="token punctuation">,</span> names<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>newStr<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>输出:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
&amp;lt;a href='https://www.baidu.com/cn666666/?p=1' target='_blank'&gt;学习以下内容&amp;lt;/a&gt;
&amp;lt;a href='https://www.baidu.com/cn666666/?p=125' target='_blank'&gt;学习以下内容&amp;lt;/a&gt;
&amp;lt;a href='https://www.baidu.com/cn666666/?p=33' target='_blank'&gt;学习以下内容&amp;lt;/a&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>sub 方法就是也是替换 字符串， 但是被替换的内容 用 正则表达式来表示 符合特征的所有字符串。</p> <p>比如:</p> <ul><li>第一个参数 <span style="color:red;font-weight:bold;"> /(av\d+?)/ </span> 这个正则表达式，表示以 <span style="color:red;font-weight:bold;"> /av </span> 开头，后面是一串数字，再以 <span style="color:red;font-weight:bold;"> / </span> 结尾的 这种特征的字符串 ，是需要被替换的。</li> <li>第二个参数，这里 是 '/cn666666/' 这个字符串，表示用什么来替换。</li> <li>第三个参数是 源字符串。</li></ul> <h3 id="指定替换函数"><a href="#指定替换函数" class="header-anchor">#</a> 指定替换函数</h3> <p>刚才的例子中，我们用来替换的是一个固定的字符串 /cn666666/。</p> <p>如果，我们要求，替换后的内容 的是原来的数字+6， 比如 /av66771949/ 替换为 /av66771955/ 。
</p><hr> <span style="color:red;font-weight:bold;"> 怎么办？ </span><p></p> <p>这种更加复杂的替换，我们可以把 sub的第2个参数 指定为一个函数 ，该函数的返回值，就是用来替换的字符串。</p> <p><span style="color:red;font-weight:bold;"> 如下 </span></p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token keyword">import</span> re

names <span class="token operator">=</span> <span class="token triple-quoted-string string">'''&lt;a href='https://www.baidu.com/av66771949/?p=1' target='_blank'&gt;学习以下内容&lt;/a&gt;
&lt;a href='https://www.baidu.com/av46349552/?p=125' target='_blank'&gt;学习以下内容&lt;/a&gt;
&lt;a href='https://www.baidu.com/av90571967/?p=33' target='_blank'&gt;学习以下内容&lt;/a&gt;
'''</span>


<span class="token comment"># 替换函数，参数是 Match对象</span>
<span class="token keyword">def</span> <span class="token function">subFunc</span><span class="token punctuation">(</span><span class="token keyword">match</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token comment"># Match对象 的 group(0) 返回的是整个匹配上的字符串，</span>
src <span class="token operator">=</span> <span class="token keyword">match</span><span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>

    <span class="token comment"># Match对象 的 group(1) 返回的是第一个group分组的内容, 如果有多个分组, 可以一次写了处理,这里演示一个用法</span>
    number <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token keyword">match</span><span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">6</span>
    dest <span class="token operator">=</span> <span class="token string-interpolation"><span class="token string">f'/av</span><span class="token interpolation"><span class="token punctuation">{</span>number<span class="token punctuation">}</span></span><span class="token string">/'</span></span>

    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span>src<span class="token punctuation">}</span></span><span class="token string"> 替换为 </span><span class="token interpolation"><span class="token punctuation">{</span>dest<span class="token punctuation">}</span></span><span class="token string">'</span></span><span class="token punctuation">)</span>
    <span class="token comment"># 返回值就是最终替换的字符串</span>
    <span class="token keyword">return</span> dest

newStr <span class="token operator">=</span> re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span><span class="token string">r'/av(\d+?)/'</span><span class="token punctuation">,</span> subFunc <span class="token punctuation">,</span> names<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>newStr<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><p>输出:</p> <div class="language-text line-numbers-mode"><pre class="language-text"><code>F:\python\venv\Scripts\python.exe F:/python/test.py
/av66771949/ 替换为 /av66771955/
/av46349552/ 替换为 /av46349558/
/av90571967/ 替换为 /av90571973/
&amp;lt;a href='https://www.baidu.com/av66771955/?p=1' target='_blank'&gt;学习以下内容&amp;lt;/a&gt;
&amp;lt;a href='https://www.baidu.com/av46349558/?p=125' target='_blank'&gt;学习以下内容&amp;lt;/a&gt;
&amp;lt;a href='https://www.baidu.com/av90571973/?p=33' target='_blank'&gt;学习以下内容&amp;lt;/a&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><hr> <p>获取组内字符串，如下</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token keyword">match</span><span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment"># 获取整个匹配字符串</span>
<span class="token keyword">match</span><span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment"># 获取第1个组内字符串</span>
<span class="token keyword">match</span><span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment"># 获取第2个组内字符串</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>Python 3.6 以后的版本 ，写法也可以更加简洁，直接像列表一样使用下标，如下</p> <div class="language-py line-numbers-mode"><pre class="language-py"><code><span class="token keyword">match</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
<span class="token keyword">match</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
<span class="token keyword">match</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><hr> <hr> <hr> <span style="color:red;font-weight:bold;"> 上面这个例子中： </span> <p>正则表达式 re.sub 函数执行时， <span style="color:red;font-weight:bold;"> 每发现一个 </span>  匹配的子串， 就会：</p> <ul><li><p>实例化一个 match对象, 这个match 对象包含了这次匹配的信息， 比如：整个字符串是什么，匹配部分字符串是什么，里面的各个group分组 字符串是什么</p></li> <li><p>调用执行 sub函数的第2个参数对象，也就是调用回调函数subFunc, 并且把刚才产生的 match 对象作为参数传递给 subFunc</p></li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2022/5/28 15:40:26</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/share/docs/python/bug.html" class="prev">
            python常见问题
          </a></span> <span class="next"><a href="/share/docs/python/string.html">
            python常用函数
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/regular.html#入门引导案例" class="sidebar-link reco-side-入门引导案例" data-v-cb1513f6>入门引导案例</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/regular.html#正则表达式常见语法" class="sidebar-link reco-side-正则表达式常见语法" data-v-cb1513f6>正则表达式常见语法</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#普通字符" class="sidebar-link reco-side-普通字符" data-v-cb1513f6>普通字符</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/regular.html#特殊字符" class="sidebar-link reco-side-特殊字符" data-v-cb1513f6>特殊字符</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#点-匹配所有字符" class="sidebar-link reco-side-点-匹配所有字符" data-v-cb1513f6>点-匹配所有字符</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#星号-重复匹配任意次-包括0次" class="sidebar-link reco-side-星号-重复匹配任意次-包括0次" data-v-cb1513f6>星号-重复匹配任意次(包括0次)</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#加号-重复匹配多次-不包括0次" class="sidebar-link reco-side-加号-重复匹配多次-不包括0次" data-v-cb1513f6>加号-重复匹配多次(不包括0次)</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#问号-匹配0次或1次" class="sidebar-link reco-side-问号-匹配0次或1次" data-v-cb1513f6>问号-匹配0次或1次</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#花括号-匹配指定次数" class="sidebar-link reco-side-花括号-匹配指定次数" data-v-cb1513f6>花括号-匹配指定次数</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#贪婪模式和非贪婪模式" class="sidebar-link reco-side-贪婪模式和非贪婪模式" data-v-cb1513f6>贪婪模式和非贪婪模式</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#对元字符进行转义" class="sidebar-link reco-side-对元字符进行转义" data-v-cb1513f6>对元字符进行转义</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#匹配某种字符类型" class="sidebar-link reco-side-匹配某种字符类型" data-v-cb1513f6>匹配某种字符类型</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#方括号-匹配几个字符之一" class="sidebar-link reco-side-方括号-匹配几个字符之一" data-v-cb1513f6>方括号-匹配几个字符之一</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#起始、结尾位置-和-单行、多行模式" class="sidebar-link reco-side-起始、结尾位置-和-单行、多行模式" data-v-cb1513f6>起始、结尾位置 和 单行、多行模式</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#竖线-匹配其中之一" class="sidebar-link reco-side-竖线-匹配其中之一" data-v-cb1513f6>竖线-匹配其中之一</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#括号-分组" class="sidebar-link reco-side-括号-分组" data-v-cb1513f6>括号-分组</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#让点匹配换行" class="sidebar-link reco-side-让点匹配换行" data-v-cb1513f6>让点匹配换行</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/regular.html#入门案例解释" class="sidebar-link reco-side-入门案例解释" data-v-cb1513f6>入门案例解释</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/regular.html#切割字符串" class="sidebar-link reco-side-切割字符串" data-v-cb1513f6>切割字符串</a></li><li class="level-2" data-v-cb1513f6><a href="/share/docs/python/regular.html#字符串替换" class="sidebar-link reco-side-字符串替换" data-v-cb1513f6>字符串替换</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#匹配模式替换" class="sidebar-link reco-side-匹配模式替换" data-v-cb1513f6>匹配模式替换</a></li><li class="level-3" data-v-cb1513f6><a href="/share/docs/python/regular.html#指定替换函数" class="sidebar-link reco-side-指定替换函数" data-v-cb1513f6>指定替换函数</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><!----><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="left:68px;bottom:370px;display:none;" data-v-5775ee02>
      欢迎来到我的博客
    </div> <div class="operation" style="right:90px;bottom:40px;display:none;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-5775ee02></i></div> <canvas id="banniang" width="260px" height="420" class="live2d" style="right:200px;bottom:20px;opacity:0.4;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas><!----><div class="RibbonAnimation"></div><!----></div></div>
    <script src="/share/assets/js/app.706684de.js" defer></script><script src="/share/assets/js/3.4685d505.js" defer></script><script src="/share/assets/js/1.b12738fb.js" defer></script><script src="/share/assets/js/68.c6c9e158.js" defer></script><script src="/share/assets/js/9.4e81f08e.js" defer></script>
  </body>
</html>
